package com.test.mooan.labama.activity_fragment_boom.ln_boom;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseLnActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity;
import com.test.mooan.labama.activity_fragment_boom.login_boom.MobileVerificationActivity2;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.bean_boom.user_info_boom.LnApplyDetailInfo_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.view_boom.module_boom.AddBkActView_Cote_boom;
import com.test.mooan.labama.view_boom.module_boom.AddBkActView_Fr_boom;
import com.test.mooan.labama.view_boom.module_boom.ShowWltActListView_Fr_boom;
import com.test.mooan.labama.view_boom.widget_boom.TitleView_boom;

public class StatusPendingActivity extends BaseLnActivity_boom {

    private TextView amountTv;
    private TextView termTv;
    private TextView accountNumTv;
    private View walletMobileView;
    private TextView walletMobileTv;
    private ShowWltActListView_Fr_boom showAccountListView;
    private AddBkActView_Cote_boom addBankAccountView_Cote;
    private AddBkActView_Fr_boom addBankAccountView_Fr;

    private Product product;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status_pending);

        pageTag = "LOANING";

        try {
            product = (Product) getIntent().getSerializableExtra("product");
            if (product==null) product = Product.gtInstance(null);
        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }

        TitleView_boom titleView = findViewById(R.id.activity_pending_title_view);
        setBackBtn_boom(titleView.getBackIb());

        titleView.setTitle(product.gtName());

        Button refreshBtn = findViewById(R.id.activity_pending_btn_refresh);
        amountTv = findViewById(R.id.activity_pending_tv_amount);
        termTv = findViewById(R.id.activity_pending_tv_term);
        View showAccountView = findViewById(R.id.activity_pending_view_show_account);
        TextView showAccountTextTv = findViewById(R.id.activity_pending_tv_show_account_text);
        TextView showAccountNameTv = findViewById(R.id.activity_pending_tv_show_account_name);
        accountNumTv = findViewById(R.id.activity_pending_tv_show_account_num);
        walletMobileView = findViewById(R.id.activity_pending_view_wallet_mobile);
        walletMobileTv = findViewById(R.id.activity_pending_tv_wallet_mobile);
        showAccountListView = findViewById(R.id.activity_pending_view_show_account_cote);
        addBankAccountView_Cote = findViewById(R.id.activity_pending_view_add_bank_account_cote);
        addBankAccountView_Fr = findViewById(R.id.activity_pending_view_add_bank_account_fr);
        View coteTipsView = findViewById(R.id.activity_pending_view_tips_cote);
        TextView coteTipsTv = findViewById(R.id.activity_pending_tv_tips_cote);

        addBankAccountView_Cote.setProduct(product);
        addBankAccountView_Fr.setProduct(product);

        addBankAccountView_Cote.setPageTag(pageTag);
        addBankAccountView_Fr.setPageTag(pageTag);

        refreshBtn.setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_REFRESH_CLICK",product.gtProduct());
            HttpManager_boom.getInstance().requestUserProcess(product.gtProduct(),product.gtToken());
        });


        if (CountryInfo_boom.gInstance().isFrench() && (!TextUtils.isEmpty(AppShowInfo_boom.getInstance().gTransferDelayImportant()))){
            coteTipsView.setVisibility(View.VISIBLE);
            coteTipsTv.setText(AppShowInfo_boom.getInstance().gTransferDelayImportant());
        }else coteTipsView.setVisibility(View.GONE);

        if (CountryInfo_boom.gInstance().isCote()){

            if (!ProcessInfo_boom.getInstance().isNewUser())
                addBankAccountView_Cote.setVisibility(View.VISIBLE);
            else showAccountListView.setVisibility(View.VISIBLE);

        }else if (CountryInfo_boom.gInstance().isFrench()){

            if (!ProcessInfo_boom.getInstance().isNewUser())
                addBankAccountView_Fr.setVisibility(View.VISIBLE);
            else showAccountListView.setVisibility(View.VISIBLE);

        }else if (CountryInfo_boom.gInstance().isGhana()){

            showAccountView.setVisibility(View.VISIBLE);
            showAccountNameTv.setText(getString(R.string.loan_wait_account_title_gh));
            showAccountTextTv.setText(getString(R.string.loan_wait_account_text_gh));

        }else {

            showAccountView.setVisibility(View.VISIBLE);
            showAccountNameTv.setText(getString(R.string.loan_wait_account_title));
            showAccountTextTv.setText(getString(R.string.loan_wait_account_text));

        }

        onUserProcessResponse();

    }

    @Override
    public void onUserProcessResponse() {

        //手机号后置校验
        if (ProcessInfo_boom.getInstance().isNeedVerification()){
            if (CountryInfo_boom.gInstance().isCambodia()) startActivity_boom(MobileVerificationActivity.class);
            else startActivity_boom(MobileVerificationActivity2.class);
            return;
        }

        switch (ProcessInfo_boom.getInstance().gLoanStatus()){
            case Loan_Reviewing -> {
                Intent intent = new Intent(this, StatusReviewActivity.class);
                intent.putExtra("product",product);
                startActivity(intent);
                finish();
            }
            case Order_Pending -> HttpManager_boom.getInstance().requestLoanApplyDetail(product.gtProduct(),product.gtToken());
            case Loan_Rejected -> {
                Intent intent = new Intent(this, StatusRejectedActivity.class);
                intent.putExtra("product",product);
                startActivity(intent);
                finish();
            }
            case Order_Pending_Repayment, Order_Overdue_Repayment -> {
                LnMainActivity_boom.toRepayListFlag = true;
                onBackPressed();
            }
            default -> onBackPressed();
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onLoanApplyDetailResponse() {

        String currency = LnApplyDetailInfo_boom.getInstance().gCurrency();

        amountTv.setText(currency+" "+ LnApplyDetailInfo_boom.getInstance().gAmount());
        termTv.setText(LnApplyDetailInfo_boom.getInstance().gDays()+" "+getString(R.string.days));

        if (CountryInfo_boom.gInstance().isGhana()) accountNumTv.setText(LnApplyDetailInfo_boom.getInstance().gWalletMobile());
        else accountNumTv.setText(LnApplyDetailInfo_boom.getInstance().gBankAccount());

        if (CountryInfo_boom.gInstance().isCambodia()){
            walletMobileView.setVisibility(View.VISIBLE);
            walletMobileTv.setText(LnApplyDetailInfo_boom.getInstance().gWalletMobile());
        }else walletMobileView.setVisibility(View.GONE);

        if (CountryInfo_boom.gInstance().isFrench())
            HttpManager_boom.getInstance().fetchBoundBankAccount(product.gtProduct(),product.gtToken());
        else HttpManager_boom.getInstance().fetchStarWindowConfig();

    }

    //获取bankAccountList返回(科特)
    @Override
    public void onFetchBankAccountListResponse(){
        if (isClose()) return;

        if (CountryInfo_boom.gInstance().isCote()){

            if (!ProcessInfo_boom.getInstance().isNewUser())
                addBankAccountView_Cote.updateAccountList();
            else showAccountListView.updateAccountList();

        }else if (CountryInfo_boom.gInstance().isFrench()){

            if (!ProcessInfo_boom.getInstance().isNewUser())
                addBankAccountView_Fr.updateAccountList();
            else showAccountListView.updateAccountList();

        }

        HttpManager_boom.getInstance().fetchStarWindowConfig();
    }

    //添加钱包账户返回
    public void onBindBankAccountResponse(){
        AddBkActView_Cote_boom.addAccountFlag = true;
        HttpManager_boom.getInstance().fetchBoundBankAccount(product.gtProduct(),product.gtToken());
    }

    //删除账户返回
    public void onDeleteBankAccountResponse(){
        Toast.makeText(this,"Supprimé avec succès",Toast.LENGTH_SHORT).show();
        HttpManager_boom.getInstance().fetchBoundBankAccount(product.gtProduct(),product.gtToken());
    }

}